﻿'use strict';
/*userBankCtrl  Controllers */
//我的银行卡
app.controller('userBankCtrl', ['$http','$scope', "$state", '$location', '$ngBootbox','dataService',
function ($http, $scope, $state, $location, $ngBootbox, dataService) {
        var userId = $scope.userId = window.localStorage.id;
        $scope.carCode = (window.localStorage.carCode == "null") ? " " : window.localStorage.carCode;

        var bankCode = $scope.bankCode = "";
        var isVehicle = $scope.isVehicle = true;
        $scope.bankDis = true;

        var skip = $scope.skip = 0;
        var count = $scope.count = 10;

        var bankList = $scope.bankList = {
            data: [],
            enableSorting: false,
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                            { displayName: "开户银行", field: 'BankName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                            { displayName: "银行卡号", field: 'Number', minWidth: 300, enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                            { displayName: "持卡人姓名", field: 'Holder', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                            {
                                displayName: "操作",
                                field: '操作',
                                cellFilter: 'checkmark',
                                cellTemplate: '<a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.bankDel(row.entity.Number)"></a>', enableColumnMenu: false
                            }],
        };

        //初始化列表
        var getDataList = function (userId) {
            dataService.GetUserBankCards(userId)
            //$http.get(Mustache.render("/api/" + "BankCard/GetUserBankCards?userId={{userId}}", { userId: userId }))
            .then(function (res) {
                $scope.bankList.data = res.data.Response.BankCards;
                $scope.total = res.data.Response.BankCards.length;
                $scope.seeks = false;
            }, function (rej) { $scope.seeks = false; });
        };
        getDataList(userId);
        $scope.seeks = true;

        //删除
        $scope.bankDel = function (bankCardNumber) {
            $ngBootbox.confirm('确认删除此银行信息？').then(function () {
                dataService.DeleteUserBankCard(userId, bankCardNumber)
                //$http.get(Mustache.render("/api/" + "BankCard/DeleteUserBankCard?userId={{userId}}&bankCardNumber={{bankCardNumber}}", { userId: userId, bankCardNumber: bankCardNumber }))
                .then(function (res) {
                    getDataList(userId);
                }, function (rej) { });
            }, function () { return; });
        };

        $scope.bank = "";
        $scope.cardNumber = "";
        $scope.holder = "";




        //添加
        $scope.bankAdd = function () {
            $ngBootbox.confirm('确认添加此银行信息？').then(function () {
                if ($scope.total >= 10) {
                    $ngBootbox.alert("最多添加10张银行卡");
                    return;
                } else {
                    dataService.AddUserBankCard(userId,$scope.holder,$scope.cardNumber)
                    //$http.get(Mustache.render("/api/" + "BankCard/AddUserBankCard?userId={{userId}}&accountName={{accountName}}&bankCardNumber={{bankCardNumber}}", { userId: userId, accountName: $scope.holder, bankCardNumber: $scope.cardNumber }))
                    .then(function (res) {
                        $scope.bank = "";
                        $scope.cardNumber = "";
                        $scope.holder = "";
                        getDataList(userId);
                    }, function (rej) { });
                }
            }, function () { return });
        };

        //获取银行卡名称
        $scope.getBankName = function () {
            var bankLen;
            if ($scope.cardNumber == "" || $scope.cardNumber == undefined || $scope.cardNumber == null) {
                $scope.bank = "";
                bankLen = 0;
                return;
            } else {
                bankLen = $scope.cardNumber.length;
            }
            if (bankLen > 15) {
                dataService.GetBankName($scope.holder, $scope.cardNumber)
                //$http.get(Mustache.render("/api/" + "BankCard/GetBankName?accountName={{accountName}}&bankCardNumber={{bankCardNumber}}", { accountName: $scope.holder, bankCardNumber: $scope.cardNumber }))
                .then(function (res) {
                    res = res.data.Response.Value;
                    if (res == null) { res = ""; $scope.bankDis = false; };
                    $scope.bank = res;
                    $scope.bankDis = true;
                }, function (rej) {
                    $scope.bank = "";
                    $scope.bankDis = false;
                    return;
                });

            }
        };


    }]);
    